package com.aranandroid.customview.edit

import android.content.Context
import kotlin.jvm.JvmOverloads
import android.widget.LinearLayout
import android.widget.EditText
import androidx.constraintlayout.widget.ConstraintLayout
import com.aranandroid.customview.R
import android.text.TextWatcher
import android.text.Editable
import android.util.AttributeSet
import android.view.KeyEvent
import android.widget.TextView.OnEditorActionListener
import android.widget.TextView
import android.view.inputmethod.EditorInfo
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView

class SearchEdit @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) :
    LinearLayout(context, attrs) {
    private val view: View
    var search_colse: ImageView? = null
    var search_img: ImageView? = null
    var search_edit: EditText? = null
    var clmain: ConstraintLayout? = null
    var textChanged: (s: CharSequence?, start: Int, before: Int, count: Int) -> Unit =
        { s, start, before, count -> }
    var searchListenter: (v: TextView?, actionId: Int, event: KeyEvent?) -> Unit =
        { v, acrionId, event -> }
    var closeCLickListtenter: () -> Unit? = { }
    private fun initView() {
        search_colse = view.findViewById(R.id.search_colse)
        search_img = view.findViewById(R.id.search_img)
        search_edit = view.findViewById(R.id.search_edit)
        clmain = view.findViewById(R.id.clmain)
    }

    private fun initListener() {
        search_edit!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                textChanged(s, start, before, count)
            }

            override fun afterTextChanged(s: Editable) {
                if (s.length > 0) {
                    search_colse!!.visibility = VISIBLE
                } else {
                    search_colse!!.visibility = GONE
                }
            }
        })
        search_edit!!.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchListenter(v, actionId, event)
                return@OnEditorActionListener true
            }
            false
        })
        search_colse!!.setOnClickListener {
            search_edit!!.setText("")
            closeCLickListtenter()
        }
    }

    // edit的值
    var searchEdit: String?
        get() = search_edit!!.text.toString().trim { it <= ' ' }
        set(s) {
            search_edit!!.setText(s)
        }

    /**
     * 设置点击时间
     */
    fun setEditCLickListtenter(clickListenter: OnClickListener?) {
        search_edit!!.setOnClickListener(clickListenter)
    }


    /**
     * 设置可以获取焦点
     */
    fun setEditFocusable() {
        search_edit!!.isClickable = true
        search_edit!!.isFocusableInTouchMode = false
    }

    /**
     * 设置隐藏字段
     */
    fun setEditHint(s: String?) {
        search_edit!!.hint = s
    }

    init {
        view = LayoutInflater.from(context).inflate(R.layout.view_search, this)
        initView()
        initListener()
    }
}